/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * GestaoView.java
 *
 * Created on 23/11/2009, 09:23:02
 */

package view.execucao;

import java.sql.SQLException;
import java.util.Date;
import javax.swing.JOptionPane;
import javax.swing.table.TableModel;
import javax.swing.table.TableRowSorter;
import model.execucao.Agenda;
import model.execucao.GestaoDao;
import model.execucao.ProcAtivDao;
import model.execucao.Transicao;
import view.processo.ConsultaProcesso;

/**
 *
 * @author Administrador
 */
public class GestaoView extends javax.swing.JInternalFrame {

    /** Creates new form GestaoView */
    public GestaoView() {
        initComponents();
        tarefas();
        jtbtarefas.setAutoCreateRowSorter(true);
        TableRowSorter<TableModel> sorter = new TableRowSorter<TableModel>(jtbtarefas.getModel());
        jtbtarefas.setRowSorter(sorter);

    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jftcp = new javax.swing.JLabel();
        jtfproc = new javax.swing.JTextField();
        dt_inicial = new net.sf.nachocalendar.components.DatePanel();
        dt_final = new net.sf.nachocalendar.components.DatePanel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jbtsproc = new javax.swing.JButton();
        jftca = new javax.swing.JLabel();
        jtfativ = new javax.swing.JTextField();
        jbtsativ = new javax.swing.JButton();
        jftct = new javax.swing.JLabel();
        jtftarefa = new javax.swing.JTextField();
        jckfinalizadas = new javax.swing.JCheckBox();
        jckverifica = new javax.swing.JCheckBox();
        jbtpesquisar = new javax.swing.JButton();
        jcbprazo = new javax.swing.JComboBox();
        jLabel1 = new javax.swing.JLabel();
        jcb_analitico = new javax.swing.JCheckBox();
        jPanel2 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jtbtarefas = new javax.swing.JTable();
        jButton1 = new javax.swing.JButton();
        jbt_adia = new javax.swing.JButton();

        setClosable(true);
        setDefaultCloseOperation(javax.swing.WindowConstants.HIDE_ON_CLOSE);
        setTitle("Gestão de processos");
        setToolTipText("");

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Filtro"));

        jftcp.setForeground(new java.awt.Color(204, 0, 0));
        jftcp.setText("Processo :");

        jtfproc.setEditable(false);

        jLabel2.setText("Data de Início");

        jLabel3.setText("Data Término");

        jbtsproc.setText("selecionar");
        jbtsproc.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtsprocActionPerformed(evt);
            }
        });

        jftca.setText("Atividade:");

        jtfativ.setEditable(false);

        jbtsativ.setText("selecionar");
        jbtsativ.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtsativActionPerformed(evt);
            }
        });

        jftct.setText("Tarefa    :");

        jckfinalizadas.setText("Tarefas finalizadas");

        jckverifica.setText("Verifica data");

        jbtpesquisar.setText("Pesquisar");
        jbtpesquisar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtpesquisarActionPerformed(evt);
            }
        });

        jcbprazo.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Todos", "Atrasados", "Normais" }));

        jLabel1.setText("Prazo      :");

        jcb_analitico.setText("Analítico");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel2)
                    .addComponent(dt_inicial, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(26, 26, 26)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(dt_final, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(jPanel1Layout.createSequentialGroup()
                                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                        .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
                                            .addComponent(jftcp)
                                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                            .addComponent(jtfproc, javax.swing.GroupLayout.PREFERRED_SIZE, 129, javax.swing.GroupLayout.PREFERRED_SIZE))
                                        .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
                                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                                .addComponent(jftct)
                                                .addComponent(jftca))
                                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                                .addComponent(jtfativ, javax.swing.GroupLayout.DEFAULT_SIZE, 130, Short.MAX_VALUE)
                                                .addComponent(jtftarefa, javax.swing.GroupLayout.DEFAULT_SIZE, 130, Short.MAX_VALUE)
                                                .addComponent(jcbprazo, 0, 130, Short.MAX_VALUE))))
                                    .addGap(6, 6, 6))
                                .addGroup(jPanel1Layout.createSequentialGroup()
                                    .addComponent(jckfinalizadas)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)))
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(jckverifica)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jcb_analitico)
                                .addGap(23, 23, 23)))
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jbtsativ, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jbtsproc, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jbtpesquisar)))
                    .addComponent(jLabel1)))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(jPanel1Layout.createSequentialGroup()
                            .addGap(135, 135, 135)
                            .addComponent(jbtpesquisar))
                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(jftcp)
                                .addComponent(jtfproc, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(jbtsproc))
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                    .addComponent(jbtsativ)
                                    .addComponent(jtfativ, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGroup(jPanel1Layout.createSequentialGroup()
                                    .addComponent(jftca)
                                    .addGap(18, 18, 18)
                                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                        .addComponent(jftct)
                                        .addComponent(jtftarefa, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
                            .addGap(11, 11, 11)
                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(jLabel1)
                                .addComponent(jcbprazo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(jckfinalizadas)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(jckverifica)
                                .addComponent(jcb_analitico))))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel2)
                            .addComponent(jLabel3))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(dt_final, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(dt_inicial, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addGap(0, 0, 0))
        );

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Tarefas"));

        jtbtarefas.getTableHeader().setReorderingAllowed(false);
        jScrollPane1.setViewportView(jtbtarefas);

        jButton1.setText("Histórico");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jbt_adia.setText("Adiar Vencimento");
        jbt_adia.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbt_adiaActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 746, Short.MAX_VALUE)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addContainerGap(258, Short.MAX_VALUE)
                .addComponent(jButton1)
                .addGap(18, 18, 18)
                .addComponent(jbt_adia)
                .addGap(282, 282, 282))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 229, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jbt_adia)
                    .addComponent(jButton1))
                .addGap(11, 11, 11))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jPanel2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-794)/2, (screenSize.height-526)/2, 794, 526);
    }// </editor-fold>//GEN-END:initComponents

    private void jbtpesquisarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtpesquisarActionPerformed
    int cp,ca,ct,prazo;
         Date data_inicial = dt_inicial.getDate();
         Date data_final = dt_final.getDate();
         prazo = jcbprazo.getSelectedIndex();
         try
         {
           cp = Integer.valueOf( jtfproc.getText());
         }catch(NumberFormatException nfe)
         {
             cp = 0;
         }
         try
         {
           ca = Integer.valueOf( jtfativ.getText());
         }catch(NumberFormatException nfe)
         {
             ca = 0;
         }
         try
         {
           ct = Integer.valueOf( jtftarefa.getText());
         }catch(NumberFormatException nfe)
         {
             ct = 0;
         }
         try
         {

             if(jckfinalizadas.isSelected())
             {
                 if(jckverifica.isSelected())
                 {
                     new GestaoDao().pesquisaAgenda(jtbtarefas, data_inicial, data_final,cp,ca,ct,true,prazo,jcb_analitico.isSelected());
                 }
                 else
                 {
                     new GestaoDao().pesquisaAgenda(jtbtarefas, data_inicial, data_final,cp,ca,ct,false,prazo,jcb_analitico.isSelected());
                 }
             }
             else
             {
                 if(jckverifica.isSelected())
                 {
                     new GestaoDao().pesquisa(jtbtarefas, data_inicial, data_final,cp,ca,ct,true,prazo);
                 }
                 else
                 {
                     new GestaoDao().pesquisa(jtbtarefas, data_inicial, data_final,cp,ca,ct,false,prazo);
                 }
             }

         }catch(SQLException ex)
         {
             JOptionPane.showMessageDialog(null, ex.getMessage());
         }
    }//GEN-LAST:event_jbtpesquisarActionPerformed

    private void jbtsprocActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtsprocActionPerformed
        new ConsultaProcesso(null, true,this).setVisible(true);
    }//GEN-LAST:event_jbtsprocActionPerformed

    private void jbtsativActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtsativActionPerformed
        new ConsultaAtivView(null, true, this).setVisible(true);
    }//GEN-LAST:event_jbtsativActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed

        try{

            int linha = jtbtarefas.getSelectedRow();
            if(linha == -1)
            {
                JOptionPane.showMessageDialog(null, "Não há tarefa selecionada, verifique!");
                return;
            }
            int idtarefa =  Integer.valueOf(String.valueOf(jtbtarefas.getValueAt(linha,5)));
            Transicao trans= null;
            if(idtarefa == 0)
            {
              jtbtarefas.setValueAt(jtbtarefas.getValueAt(linha, 0), linha, 5);
              idtarefa =  Integer.valueOf(String.valueOf(jtbtarefas.getValueAt(linha,0)));
            }
            Agenda agenda = new ProcAtivDao().getAgenda(idtarefa);
            idtarefa =  Integer.valueOf(String.valueOf(jtbtarefas.getValueAt(linha,5)));
            //boolean vencimento = new ProcAtivDao().consultaPrazo(idtarefa);
            if(!jckfinalizadas.isSelected())
            {                
                trans = new ProcAtivDao().getTransicao(idtarefa);
            }else
            {

               trans = new Transicao(agenda.getIdtarefa(), agenda.getIdativ(), agenda.getIdproc(), 0, agenda.getIdusuario(), 0, new Date(agenda.getDt_inicial()), 0, agenda.getIdtarefa(), agenda.getPai(),agenda.getSolicitante());
            }
            //    JOptionPane.showMessageDialog(null, trans.getTransicao());
            //if(vencimento)new EscolheFinView(null, true, trans).setVisible(true);

            new HistoricoProcessoView(null, true, trans).setVisible(true);



            }catch(SQLException sql)
            {
                JOptionPane.showMessageDialog(null,"Tarefa ainda não possui histórico, verifique!","Verifique",1);
            }


    }//GEN-LAST:event_jButton1ActionPerformed

    private void jbt_adiaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbt_adiaActionPerformed
            try{

            int linha = jtbtarefas.getSelectedRow();
            if(linha == -1)
            {
                JOptionPane.showMessageDialog(null, "Não há tarefa selecionada, verifique!");
                return;
            }
            int idtarefa =  Integer.valueOf(String.valueOf(jtbtarefas.getValueAt(linha,5)));
            Transicao trans= null;
            if(idtarefa == 0)
            {
              jtbtarefas.setValueAt(jtbtarefas.getValueAt(linha, 0), linha, 5);
              idtarefa =  Integer.valueOf(String.valueOf(jtbtarefas.getValueAt(linha,0)));
            }
            Agenda agenda = new ProcAtivDao().getAgenda(idtarefa);
            idtarefa =  Integer.valueOf(String.valueOf(jtbtarefas.getValueAt(linha,0)));
            //boolean vencimento = new ProcAtivDao().consultaPrazo(idtarefa);
            if(!jckfinalizadas.isSelected())
            {
                trans = new ProcAtivDao().getTransicao(idtarefa);
            }else
            {

               //trans = new Transicao(agenda.getIdtarefa(), agenda.getIdativ(), agenda.getIdproc(), 0, agenda.getIdusuario(), 0, new Date(agenda.getDt_inicial()), 0, agenda.getIdtarefa(), agenda.getPai(),agenda.getSolicitante());
            }
            //    JOptionPane.showMessageDialog(null, trans.getTransicao());
            //if(vencimento)new EscolheFinView(null, true, trans).setVisible(true);

            //new HistoricoProcessoView(null, true, trans).setVisible(true);
            new AdiaTarefasView(null, true, trans).setVisible(true);


            }catch(SQLException sql)
            {
                JOptionPane.showMessageDialog(null,sql.getMessage(),"Verifique",1);
            }
    }//GEN-LAST:event_jbt_adiaActionPerformed

    private void tarefas()
    {
        new GestaoDao().carregaTarefas(jtbtarefas);
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private net.sf.nachocalendar.components.DatePanel dt_final;
    private net.sf.nachocalendar.components.DatePanel dt_inicial;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JButton jbt_adia;
    private javax.swing.JButton jbtpesquisar;
    private javax.swing.JButton jbtsativ;
    private javax.swing.JButton jbtsproc;
    private javax.swing.JCheckBox jcb_analitico;
    private javax.swing.JComboBox jcbprazo;
    private javax.swing.JCheckBox jckfinalizadas;
    private javax.swing.JCheckBox jckverifica;
    private javax.swing.JLabel jftca;
    private javax.swing.JLabel jftcp;
    private javax.swing.JLabel jftct;
    private javax.swing.JTable jtbtarefas;
    public javax.swing.JTextField jtfativ;
    public javax.swing.JTextField jtfproc;
    public javax.swing.JTextField jtftarefa;
    // End of variables declaration//GEN-END:variables

}
